package com.example.element.module.netWork

import android.content.Context
import android.content.Intent
import com.example.common.base.AbsActivity
import com.example.common.utils.L
import com.example.element.R
import com.example.element.databinding.ActNewworkOkhttpBinding
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONArray
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserFactory
import java.io.StringReader
import kotlin.concurrent.thread

/**
 * OkHttp,xml-pull,json
 */
class OkHttpActivity : AbsActivity<ActNewworkOkhttpBinding>() {

    override fun getLayoutId() = R.layout.act_newwork_okhttp

    companion object {

        @JvmStatic
        fun open(context: Context) {
            context.startActivity(Intent(context, OkHttpActivity::class.java))
        }
    }

    override fun main() {

    }

    override fun initListener() {
        mBinding.btnHttpUrl.setOnClickListener {
            thread {
                val okHttpClient = OkHttpClient()
                val request = Request.Builder()
                        .url("http://192.168.1.121:8080/get_data.xml")
                        .build()
                val response = okHttpClient.newCall(request).execute()
                val responseData = response.body?.string()
                if (responseData != null) {
                    runOnUiThread {
                        mBinding.tvText.text = responseData
                        parseXmlWithPull(responseData)
                    }
                }

            }
        }

        mBinding.btnJson.setOnClickListener {
            HttpUtil.sendHttpRequest2("http://192.168.1.121:8080/json_data.json",object :HttpCallBackListener{
                override fun onFinish(response: String) {
                    runOnUiThread {
                        mBinding.tvText.text = response
                        parseJson(response)
                    }
                }

                override fun onError(e: Exception) {
                }

            })
        }
    }

    private fun parseJson(jsonData: String) {
        try {
            val jsonArray = JSONArray(jsonData)
            for (i in 0 until jsonArray.length()) {
                val jsonObject = jsonArray.getJSONObject(i)
                val id = jsonObject.getString("id")
                val name = jsonObject.getString("name")
                val version = jsonObject.getString("version")
                L.e(TAG, "id==$id,name==$name,version==$version")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    private fun parseXmlWithPull(xmlData: String) {
        var id = ""
        var name = ""
        var version = ""
        val factory = XmlPullParserFactory.newInstance()
        val xmlPullParser = factory.newPullParser()
        xmlPullParser.setInput(StringReader(xmlData))
        var eventType = xmlPullParser.eventType
        while (eventType != XmlPullParser.END_DOCUMENT) {
            val nodeName = xmlPullParser.name
            L.e(TAG, "nodeName==$nodeName")
            when (eventType) {
                //开始解析某个节点
                XmlPullParser.START_TAG -> {
                    when (nodeName) {
                        "id" -> id = xmlPullParser.nextText()
                        "name" -> name = xmlPullParser.nextText()
                        "version" -> version = xmlPullParser.nextText()
                    }
                }
                //完成解析某个节点
                XmlPullParser.END_TAG -> {
                    if ("app" == nodeName) {
                        L.e(TAG, "id==$id")
                        L.e(TAG, "name==$name")
                        L.e(TAG, "version==$version")
                    }
                }
            }
            eventType = xmlPullParser.next()
        }
    }

}